var _IA300Client;
var _TimerErrorMessage;
var _ExpireUrl;

function isIe() {
    return ("ActiveXObject" in window);
}

function IA300_GetInstance() {
    if (_IA300Client == null) {
        if (isIe()) {	//IE
            _IA300Client = new IA300ClientmPlugin();
        }
        else {
            _IA300Client = new IA300ClientmPlugin();
        }
    }

    _IA300Client.Model = 0;
    return _IA300Client;
}

function IA300_CheckPassword(password) {
    return IA300_GetInstance().IA300Open(password);
}

function IA300_LogOut() {
    return IA300_GetInstance().IA300Close();
}

function IA300_GetLastError() {
    return IA300_GetInstance().IA300GetLastError();
}

function IA300_GetHardwareId() {
    return IA300_GetInstance().IA300GetUID();
}

function IA300_RequestEnabled() {
    if (IA300_GetInstance() instanceof IA300ClientmPlugin) {
        return IA300_GetInstance().RequestEnabled();
    }
    else {
        return IA300_GetInstance().RequestEnabled;
    }
}

function IA300_SHA1WithSeedEx(RanDoms) {
    return IA300_GetInstance().IA300SHA1WithSeedEx(RanDoms);
}

function IA300_GetMachineCode() {
    return IA300_GetInstance().IA300GetMachineCode();
}


function IA300_CalculateClientHash(randomMessageFromServer) {
    return IA300_GetInstance().IA300SHA1WithSeed(randomMessageFromServer);
}


function IA300_CheckExist() {
    var rtn = IA300_GetInstance().IA300CheckExist();
    if (rtn < 1) {
        IA300_LogOut();
        if (_TimerErrorMessage != null) {
            alert(_TimerErrorMessage + "  Error Code: " + IA300Client.IA300GetLastError());
        }
        if (_ExpireUrl != null) {
            window.location = _ExpireUrl;
        }
    }
    return rtn;
}

function IA300_StartCheckTimer(interval, errMsg, logonUrl) {
    _TimerErrorMessage = errMsg;
    _ExpireUrl = logonUrl;
    window.setInterval(IA300_CheckExist, interval);
}


function IA300_ChangePassword(oldPassword, newPassword, newPasswordConfirm) {
    if (oldPassword == "") {
        return 1;
    }
    if (newPassword != newPasswordConfirm) {
        return 2;
    }
    if (0 != IA300_GetInstance().IA300ChangePassword(oldPassword, newPassword)) {
        return 3;
    }
    return 0;
}

function IA300_ResetPasswordRequest() {
    return IA300_GetInstance().IA300GenResetPwdRequest();
}

function IA300_ResetPassword(serverResponse) {
    return IA300_GetInstance().IA300ResetPassword(serverResponse);
}


function IA300_WriteUserStorage(UserStorage) {
    return IA300_GetInstance().IA300WriteUserStorage(UserStorage);
}


function IA300_ReadUserStorage(length) {
    return IA300_GetInstance().IA300ReadUserStorage(length);
}


function IA300_WriteStorage(storage) {
    return IA300_GetInstance().IA300SecureWriteStorage(storage);
}

function IA300_ReadStorage(storage) {
    return IA300_GetInstance().IA300SecureReadStorage(storage);
}

function IA300_WriteUserStorageEx(nStartAddr, pBuffer) {
    return IA300_GetInstance().IA300WriteUserStorageEx(nStartAddr, pBuffer);
}

function IA300_ReadUserStorageEx(nStartAddr, nDataLen) {
    return IA300_GetInstance().IA300ReadUserStorageEx(nStartAddr, nDataLen);
}

function IA300_SecureWriteStorageEx(nStartAddr, pBuffer) {
    return IA300_GetInstance().IA300SecureWriteStorageEx(nStartAddr, pBuffer);
}

function IA300_SecureReadStorageEx(nStartAddr, nDataLen) {
    return IA300_GetInstance().IA300SecureReadStorageEx(nStartAddr, nDataLen);
}

function IA300_DataEncrypt(pBuffer) {
    return IA300_GetInstance().IA300DataEncrypt(pBuffer);
}

function IA300_RemoteChangeRequest(strRandom) {
    var rtn = IA300_GetInstance().IA300CheckExist();
    if (rtn == 0) {
        return;
    }
    return IA300_GetInstance().IA300RemoteChangeRequest(strRandom);
}

function IA300_RemoteChange(strResponse, strRandom) {
    var rtn = IA300_GetInstance().IA300CheckExist();
    if (rtn == 0) {
        return;
    }
    return IA300_GetInstance().IA300RemoteChange(strResponse, strRandom);
}

function IA300_GetName() {
    if (IA300_GetInstance() instanceof IA300ClientmPlugin) {
        return IA300_GetInstance().Name();
    }
    else {
        return IA300_GetInstance().Name;
    }
}
function IA300_GetDescription() {
    if (IA300_GetInstance() instanceof IA300ClientmPlugin) {
        return IA300_GetInstance().Description();
    }
    else {
        return IA300_GetInstance().Description;
    }
}
function IA300_GetUrl() {
    if (IA300_GetInstance() instanceof IA300ClientmPlugin) {
        return IA300_GetInstance().Url();
    }
    else {
        return IA300_GetInstance().Url;
    }
}
function IA300_GetOther() {
    if (IA300_GetInstance() instanceof IA300ClientmPlugin) {
        return IA300_GetInstance().Other();
    }
    else {
        return IA300_GetInstance().Other;
    }
}
function createElementIA300() {

}

function DetectIA300Plugin() {
    var browser = DetectBrowser();
    if (browser == "IE") {
        return true;
    }
    else {
        return true;
    }

}

function DetectBrowser() {
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
        (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
            (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
                (s = ua.match(/rv:([\d.]+)/)) ? Sys.ie = s[1] :
                    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] :
                        (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;

    var browser = "Unknown";
    if (Sys.ie) { browser = "IE"; }
    if (Sys.firefox) { browser = "Firefox"; }
    if (Sys.chrome) { browser = "Chrome"; }
    if (Sys.opera) { browser = "Opera"; }
    if (Sys.safari) { browser = "Safari"; }

    return browser;
}

function IsIE9Above() {
    if (navigator.userAgent.indexOf("Gecko") >= 0) {
        return true;
    }
    else {
        var ua = navigator.userAgent.toLowerCase().match(/msie ([\d.]+)/)[1];
        if (parseInt(ua, 10) >= 9)
            return true;
        else
            return false;
    }
}

function IA300ClientmPlugin() {
    var url = "http://127.0.0.1:53005/ia300";

    var xmlhttp;
    function AjaxIO(json) {
        if (xmlhttp == null) {
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            } else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        if ("https:" == document.location.protocol) {
            url = "https://127.0.0.1:53015/ia300";
        }
        xmlhttp.open("POST", url, false);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("json=" + json);
    }

    this.IA300Open = function (password) {
        var json = '{"function":"IA300Open", "passWd":"' + password + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.rtn;
        } else {
            return 1;
        }
    }

    this.IA300Close = function () {
        var json = '{"function":"IA300Close"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.errorCode;
        } else {
            return -2;
        }
    }

    this.IA300GetLastError = function () {
        var json = '{"function":"IA300GetLastError"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.errorCode;
        } else {
            return -2;
        }
    }

    this.IA300GetUID = function () {
        var json = '{"function":"IA300GetUID"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return null;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.HardwareID;
        } else {
            return null;
        }
    }

    this.RequestEnabled = function () {
        var json = '{"function":"RequestEnabled"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.ReqEnabled;
        } else {
            return 0;
        }
    }

    this.IA300SHA1WithSeedEx = function (RanDoms) {
        var json = '{"function":"IA300SHA1WithSeedEx", "Seed":"' + RanDoms + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        };
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.digest;
        } else {
            return "";
        }
    }

    this.IA300GetMachineCode = function () {
        var json = '{"function":"IA300GetMachineCode"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.machineCode;
        } else {
            return "";
        }
    }

    this.IA300SHA1WithSeed = function (randomMessageFromServer) {
        var json = '{"function":"IA300SHA1WithSeed", "Seed":"' + randomMessageFromServer + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.digest;
        } else {
            return "";
        }
    }

    this.IA300CheckExist = function () {
        var json = '{"function":"IA300CheckExist"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.devCount;
        } else {
            return -2;
        }
    }

    this.IA300_StartCheckTimer = function (interval, errMsg, logonUrl) {
        _TimerErrorMessage = errMsg;
        _ExpireUrl = logonUrl;
        window.setInterval(IA300_CheckExist, interval);
    }

    this.IA300ChangePassword = function (oldPassword, newPassword, newPasswordConfirm) {
        if (oldPassword == "") {
            return 1;
        }
        if (newPassword == "") {
            return 2;
        }

        var json = '{"function":"IA300ChangePassword", "oldUpin":"' + oldPassword + '", "newUpin":"' + newPassword + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.rtn;
        } else {
            return 3;
        }

    }

    this.IA300GenResetPwdRequest = function () {
        var json = '{"function":"IA300GenResetPwdRequest"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.request;
        } else {
            return "";
        }
    }

    this.IA300ResetPassword = function (serverResponse) {
        var json = '{"function":"IA300ResetPassword", "response":"' + serverResponse + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.rtn;
        } else {
            return 1;
        }
    }

    this.IA300WriteUserStorage = function (UserStorage) {
        var json = '{"function":"IA300WriteUserStorage", "inData":"' + UserStorage + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.rtn;
        } else {
            return 1;
        }
    }

    this.IA300ReadUserStorage = function (length) {
        var json = '{"function":"IA300ReadUserStorage", "readLength":' + length + '}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.IA300SecureWriteStorage = function (storage) {
        var json = '{"function":"IA300SecureWriteStorage", "inData":"' + storage + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.rtn;
        } else {
            return 1;
        }
    }

    this.IA300SecureReadStorage = function (storage) {
        var json = '{"function":"IA300SecureReadStorage", "readLength":' + storage + '}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.IA300WriteUserStorageEx = function (nStartAddr, pBuffer) {
        if (nStartAddr.length < 1)
            nStartAddr = 0;

        var json = '{"function":"IA300WriteUserStorageEx", "startAddr":' + nStartAddr + ',"inData":"' + pBuffer + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.rtn;
        } else {
            return 1;
        }
    }

    this.IA300ReadUserStorageEx = function (nStartAddr, nDataLen) {
        if (nStartAddr.length < 1)
            nStartAddr = 0;
        var json = '{"function":"IA300ReadUserStorageEx", "startAddr":' + nStartAddr + ',"readLength":' + nDataLen + '}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.IA300SecureWriteStorageEx = function (nStartAddr, pBuffer) {
        if (nStartAddr.length < 1)
            nStartAddr = 0;
        var json = '{"function":"IA300SecureWriteStorageEx", "startAddr":' + nStartAddr + ',"inData":"' + pBuffer + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.rtn;
        } else {
            return 1;
        }
    }

    this.IA300SecureReadStorageEx = function (nStartAddr, nDataLen) {
        if (nStartAddr.length < 1)
            nStartAddr = 0;
        var json = '{"function":"IA300SecureReadStorageEx", "startAddr":' + nStartAddr + ',"readLength":' + nDataLen + '}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.IA300DataEncrypt = function (pBuffer) {
        var json = '{"function":"IA300DataEncrypt", "inData":"' + pBuffer + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.IA300RemoteChangeRequest = function (strRandom) {
        var rtn = this.IA300CheckExist();
        if (rtn == 0) {
            return;
        }

        var json = '{"function":"IA300RemoteChangeRequest", "inData":"' + strRandom + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.IA300RemoteChange = function (strResponse, strRandom) {
        var rtn = this.IA300CheckExist();
        if (rtn == 0) {
            return;
        }

        var json = '{"function":"IA300RemoteChange", "random":"' + strRandom + '", "response":"' + strResponse + '"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return -3;
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.rtn;
        } else {
            return 1;
        }
    }

    this.Name = function () {
        var json = '{"function":"IA300GetName"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.Description = function () {
        var json = '{"function":"IA300GetDescription"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.Url = function () {
        var json = '{"function":"IA300GetUrl"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

    this.Other = function () {
        var json = '{"function":"IA300GetOther"}';
        try {
            AjaxIO(json);
        }
        catch (e) {
            return "";
        }
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj = eval("(" + xmlhttp.responseText + ")");
            return obj.outData;
        } else {
            return "";
        }
    }

}